package sepe.chenli.unit4.task4_4;

import sepe.chenli.unit4.task4_3.HireUser;
import sepe.chenli.unit4.task4_3.HireUserDAOImp;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HrieUserTest3 {
    public boolean isDigit(String input) {
        boolean flag = true;
        char[] inputs = input.toCharArray();
        for (int i = 0; i < inputs.length; i++) {
            char c = inputs[i];
            if (!Character.isDigit(c)) {
                flag = false;
                break;
            } else {
                continue;
            }
        }
        return flag;
    }

    public boolean checkEmail(String email) {
        boolean flag = false;
        try {
            String check = "^([a-z0-9A-Z]+[-|.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?.)+" +
                    "[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    public boolean checkQQ(String myQQstr) {
        boolean flag = false;
        String regex = "[1-9][0-9]{4,14}";
        if (myQQstr.matches(regex)) {
            flag = true;
        }
        return flag;
    }

    public boolean checkCardID(String cardID, String dayStr) {
        boolean flag = true;
        if (cardID.length() == 18) {
            String yearStr = cardID.substring(6, 10);
            String monthStr = cardID.substring(12, 14);
            flag = this.checkDate(yearStr, monthStr, dayStr);
        } else {
            System.out.println("你输入的身份证号码: " + cardID + "长度不动");
        }
        return flag;
    }

    public boolean checkDate(String yearStr, String monthStr, String dayStr) {
        boolean flag = true;
        int year = Integer.parseInt(yearStr);
        System.out.println("year=" + year);
        if (year < 1930 || year > 2013) {
            flag = false;
            System.out.println("你输入的身份证年份不在1930-2013之间");
        }
        char c = monthStr.charAt(0);
        if (c == '0') {
            monthStr = monthStr.substring(1);
        }
        int month = Integer.parseInt(monthStr);
        if (month < 1 || month > 12) {
            flag = false;
            System.out.println("你输入的身份证的月份不正确！");
        }
        char d = dayStr.charAt(0);
        if (d == '0') {
            dayStr = dayStr.substring(1);
        }
        int day = Integer.parseInt(dayStr);
        GregorianCalendar gc = new GregorianCalendar(year, month, day);
        boolean isLeap = gc.isLeapYear(year);
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            if (day < 1 || day > 31) {
                flag = false;
                System.out.println("你的身份证中的日期不正确");
            }
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            if (day < 1 || day > 30) {
                flag = false;
                System.out.println("你的身份证中的日期不正确");
            }
        }
        if (isLeap && month == 2) {
            if (day < 1 || day > 29) {
                flag = false;
                System.out.println("你的身份证日期不正确");
            }
        }
        if (isLeap && month == 2) {
            if (day < 1 || day > 28) {
                flag = false;
                System.out.println("你的身份证日期不正确");
            }

        }
        return flag;
    }

    public Date strTransDate(String inputDate) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = dateFormat.parse(inputDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public HireUser inutHireUserInfo(String dayStr) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入求租客户编号");
        int userID = sc.nextInt();
        System.out.println("请输入求租客户姓名");
        String userName = sc.next();
        System.out.println("请输入求租客户性别");
        String sexStr = sc.next();
        char sex = sexStr.charAt(0);
        System.out.println("请输入求租客户电话");
        String phone = sc.next();
        boolean checkPhone = this.isDigit(phone);
        if (!checkPhone) {
            System.out.println("你输入的电话不正确");
        }
        System.out.println("请输入求租客户家庭电话");
        String homephone = sc.next();
        System.out.println("请输入求租客户邮箱");
        String email = sc.next();
        boolean checkEmail = this.checkEmail(email);
        if (!checkEmail) {
            System.out.println("你输入的邮箱不正确");
        }
        System.out.println("请输入求租客QQ号码");
        String qq = sc.next();
        boolean checkQq = this.checkQQ(qq);
        if (!checkQq) {
            System.out.println("你输入QQ好不正确");
        }
        System.out.println("请输入求租客身份证");
        String cardID = sc.next();
        boolean checkCardID = this.checkCardID(cardID, dayStr);
        if (!checkCardID) {
            System.out.println("你输入的身份证号码不正确");
        }
        System.out.println("请输入求租客编号");
        String hirePersonNo = sc.next();
        System.out.println("请输入记录日期，格式：yyyy-MM-dd");
        String recordDateStr = sc.next();
        Date recordDate = this.strTransDate(recordDateStr);
        HireUser hireUser = new HireUser();
        hireUser.getUserID(userID);
        hireUser.setUserName(userName);
        hireUser.setSex(sex);
        hireUser.setPhone(phone);
        hireUser.setHomePhone(homephone);
        hireUser.setEmail(email);
        hireUser.setQq(qq);
        hireUser.setCardID(cardID);
        hireUser.setHirePersonNo(hirePersonNo);
        hireUser.setRecordDate(recordDate);
        return hireUser;
    }
    public static void main(String[] args) {
            HrieUserTest3 ht=new HrieUserTest3();
            System.out.println("添加求租客户信息");
            HireUser hireUser=ht.inutHireUserInfo("");
            HireUserDAOImp hireUserDAOImp=new HireUserDAOImp();
            boolean isExist=hireUserDAOImp.isExist(hireUser,1);
            if(isExist) {
                System.out.println("你的输入有错，该用户已经存在");
            }else {
                hireUserDAOImp.insertUser(hireUser);
                System.out.println("添加求租客户信息成功");
            }
    }
}

